package com.phb.puhuibao.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.pub.entity.annotation.PrimaryKey.PK;
import com.idp.pub.service.IBaseService;
import com.idp.pub.web.controller.BaseController;
import com.phb.puhuibao.entity.MobileUser;
import com.phb.puhuibao.entity.MuserJobRelease;
import com.phb.puhuibao.entity.PhbNumber;
import com.phb.puhuibao.service.MobileUserService;
import com.phb.puhuibao.service.MuserJobReleaseService;
import com.phb.puhuibao.service.PhbNumberService;

@Controller
@RequestMapping(value = "/phbNumber")
public class PhbNumberController extends BaseController<PhbNumber, String> {
	@Resource(name = "phbNumberService")
	public void setBaseService(IBaseService<PhbNumber, String> baseService) {
		super.setBaseService(baseService);
	}
	@Resource(name = "muserJobReleaseService")
	private IBaseService<MuserJobRelease, String> muserJobReleaseService;
	@Resource(name = "mobileUserService")
	private IBaseService<MobileUser, String> mobileUserService;
	@Resource(name = "phbNumberService")
	private PhbNumberService phbNumberService;
	//-----报名列表展现-----
	@RequestMapping(value = "getAll")
	@ResponseBody
	public Map<String, Object> getAll(@RequestParam String jobId , @RequestParam String userId) {
		Map<String, Object> data = new HashMap<String, Object>();	
		Map<String, Object> dataValue = new HashMap<String, Object>();	
		try {
			List<Map<String, Object>> signUps = phbNumberService.getSignUps(jobId);	
			Map<String, Object> params = new HashMap<String, Object>();	
			params.put("jobId", jobId);
			if(muserJobReleaseService.unique(params).getmUserId()==Integer.parseInt(userId)){
				data.put("canSelect", true);
			}else{
				data.put("canSelect", false);				
			}
			dataValue.put("signUps", signUps);
			dataValue.put("count", signUps.size());
			data.put("result", dataValue);
			data.put("message", "查询报名列表成功！");
			data.put("status", 1);
		} catch (Exception e) {
			data.put("message", "查询报名列表失败！");
			data.put("status", 0);		
		}		
		return data;
	}
	//-----用户报名-----
	@RequestMapping(value = "signUp")
	@ResponseBody
	public Map<String, Object> signUp(@RequestParam String userId , @RequestParam String jobId) {
		Map<String, Object> data = new HashMap<String, Object>();
		try {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		MobileUser user = mobileUserService.unique(params);
		if(user==null){
			data.put("message", "注册并成功登陆后再来报名吧！");
			data.put("status", 0);
			return data;
		}	
		params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("jobId", jobId);
		PhbNumber number=this.getBaseService().unique(params);
		if(number!=null){
			data.put("message", "您已经报名了，去看看其他职位吧！");
			data.put("status", 0);
			return data;	
		}
		PhbNumber signUp = new PhbNumber();
		signUp.setmUserId(Integer.parseInt(userId));
		signUp.setJobId(Integer.parseInt(jobId));
		phbNumberService.processSignUp(signUp);
		    data.put("message", "报名成功！");
			data.put("status", 1);
		} catch (Exception e) {
			data.put("message", "报名失败！");
			data.put("status", 0);		
		}		
		return data;
	}
	//-----挑选报名人员-----
	@RequestMapping(value = "chooseStaffs")
	@ResponseBody
	public Map<String, Object> chooseStaffs(@RequestParam String numIds ) {
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			phbNumberService.processChooseStaffs(numIds);
			data.put("message", "保存成功！");
			data.put("status", 1);			
			} catch (Exception e) {
				data.put("message", "保存失败！");
				data.put("status", 0);		
			}		
		
		return data;
	}
	
}
